import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { userSelect } from '/@/api/backs/system/user';
import moment from 'moment';
let users: any[] = [];

async function getUserList() {
  const res = await userSelect({});
  users = res;
}
getUserList();
export const columns: BasicColumn[] = [
  {
    title: '编号',
    dataIndex: 'id',
  },
  {
    title: '组名',
    dataIndex: 'name',
  },
  {
    title: '描述',
    dataIndex: 'description',
  },
  {
    title: '成员',
    dataIndex: 'memberUserIds',
    customRender: ({ record, text }) => {
      const names: any[] = [];
      if (text) {
        for (const userId of record.memberUserIds) {
          let isUser = false;
          users.forEach((user) => {
            if (userId === user.id) {
              names.push(user.nickname);
              isUser = true;
            }
          });
        }
        return names.join();
      }
    },
  },
  {
    title: '状态',
    dataIndex: 'remark',
    customRender: ({ record }) => {
      switch (record.status) {
        case 0:
          return '启用';
          break;
        case 1:
          return '禁用';
          return;
      }
    },
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    customRender: ({ record }) => {
      const time = record.createTime;
      return time ? moment(time).format('YYYY-MM-DD') : '-';
    },
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '组名',
    component: 'Input',
    colProps: { span: 4 },
    componentProps: ({ tableAction }) => {
      return {
        onPressEnter: () => {
          const { reload } = tableAction;
          reload();
        },
      };
    },
  },
  {
    field: 'status',
    label: '状态',
    component: 'Select',
    colProps: { span: 4 },
    componentProps: {
      options: [
        {
          value: 0,
          label: '启用',
        },
        {
          value: 1,
          label: '禁用',
        },
      ],
    },
  },
  {
    field: 'createTime',
    label: '创建时间',
    component: 'RangePicker',
    colProps: { span: 8 },
    componentProps: {
      valueFormat: 'X',
    },
  },
];
export const formSchema: FormSchema[] = [
  {
    field: 'name',
    label: '组名',
    component: 'Input',
    required: true,
  },

  {
    field: 'description',
    label: '描述',
    component: 'Input',
    required: true,
  },
  {
    label: '成员',
    field: 'memberUserIds',
    component: 'ApiSelect',
    required: true,
    componentProps: {
      api: userSelect,
      labelField: 'nickname',
      valueField: 'id',
      mode: 'multiple',
    },
  },
  {
    field: 'status',
    label: '状态',
    component: 'Select',
    required: true,
    colProps: { span: 8 },
    componentProps: {
      options: [
        {
          value: 0,
          label: '启用',
        },
        {
          value: 1,
          label: '禁用',
        },
      ],
    },
  },
];
